unit EditWinPercentage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, Oracle, uSQLs, uUtils, gnugettext, umainutils;

type
  TuFrmEditWinPercentage = class(TForm)
    edt_Index: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    edt_Percentage: TLabeledEdit;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    m_IsClone, m_IsEdit: boolean;
    m_recordID: string;
  public
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string);
    { Public declarations }
  end;

var
  uFrmEditWinPercentage: TuFrmEditWinPercentage;

implementation

uses UfrmMain;
{$R *.dfm}
{ TuFrmEditWinPercentage }

procedure TuFrmEditWinPercentage.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditWinPercentage.btn_SaveClick(Sender: TObject);
begin
  if m_IsClone or (not m_IsEdit) then
  begin
    InsertDataIntoDB('WIN_PERCENTAGE', 'PERCENTAGE,SEQ_INDEX,WIN_PLAYERS_NUMBER_ID', '''' + edt_Percentage.Text + ''', ''' + edt_Index.Text + ''', ''' + FrmMain.stg_WinNumPlayers.Cells
        [0, FrmMain.stg_WinNumPlayers.Row] + '''');
  end
  else
  begin
    UpdateDataToDB('WIN_PERCENTAGE', 'PERCENTAGE=''' + edt_Percentage.Text + ''',SEQ_INDEX=''' + edt_Index.Text + ''',WIN_PLAYERS_NUMBER_ID=''' + FrmMain.stg_WinNumPlayers.Cells
        [0, FrmMain.stg_WinNumPlayers.Row] + '''', 'ID=' + m_recordID);
  end;
  FrmMain.GetGridData(FrmMain.stg_WinPercentage, SQL_WinPerc + FrmMain.stg_WinNumPlayers.Cells[0, FrmMain.stg_WinNumPlayers.Row] + ' ORDER BY SEQ_INDEX ASC', true);
  FrmMain.stg_WinPercentageSelectCell(self, 0, FrmMain.stg_WinPercentage.Row, canselect);
  FrmMain.SumWinPercentage();
  close();
end;

procedure TuFrmEditWinPercentage.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditWinPercentage.FormShow(Sender: TObject);
begin
  edt_Percentage.SetFocus;
end;

procedure TuFrmEditWinPercentage.ShowForm(IsEdit, IsClone: boolean; recordID: string);
var
  Query: string;
  SequenceIdx, MaxPlayers: integer;

begin
  m_IsClone := IsClone;
  m_IsEdit := IsEdit;
  m_recordID := recordID;
  MaxPlayers := strtoint(FrmMain.stg_WinNumPlayers.Cells[2, FrmMain.stg_WinNumPlayers.Row]);
  OracleQuery1.Session := FrmMain.OracleSession1;
  Query := 'SELECT MAX(SEQ_INDEX) FROM WIN_PERCENTAGE WHERE WIN_PLAYERS_NUMBER_ID=' + FrmMain.stg_WinNumPlayers.Cells[0, FrmMain.stg_WinNumPlayers.Row];
  OracleQuery1.SQL.Clear;
  OracleQuery1.SQL.Text := (Query);
  OracleQuery1.Execute;
  SequenceIdx := OracleQuery1.Field(0);
  SequenceIdx := SequenceIdx + 1;
  edt_Index.Text := IntToStr(SequenceIdx);
  if not IsEdit or IsClone then
  begin
    if MaxPlayers < SequenceIdx then
    begin
      ShowMessage(_('Number of percentages is reached max number of players'));
      exit;
    end;
  end;
  edt_Percentage.Text := '';
  if IsEdit then
  begin
    Query := 'select * from WIN_PERCENTAGE where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    edt_Percentage.Text := OracleQuery1.Field('PERCENTAGE');
    if m_IsClone then
    begin
      edt_Index.Text := IntToStr(SequenceIdx + 1);
    end
    else
    begin
      edt_Index.Text := OracleQuery1.Field('SEQ_INDEX');
    end;
  end;
  ShowModal();
end;

end.
